package com.handwriting.backend.controller;

import com.handwriting.backend.entity.Model;
import com.handwriting.backend.message.Response;
import com.handwriting.backend.message.TrainParams;
import com.handwriting.backend.message.TrainingProgress;
import com.handwriting.backend.service.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
public class ModelController {
    private final ModelService service;

    @Autowired
    public ModelController(ModelService service) {
        this.service = service;
    }

    @Operation(summary = "测试图片")
    @PostMapping(value = "/model/test", consumes = "multipart/form-data")
    public Response<Integer> modelTest(@RequestPart("image") MultipartFile image) {
        try {
            int testResult = service.testModel(image);
            return Response.ok(testResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error testing image.");
        }
    }

    @Operation(summary = "获取所有模型")
    @GetMapping(value = "/model/info")
    public Response<List<Model>> modelInfo() {
        try {
            List<Model> models = service.getAllModelInfo();
            return Response.ok(models);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Cannot get model information");
        }
    }

    @Operation(summary = "选择当前模型")
    @PostMapping(value = "/model/select/{id}")
    public Response<Object> selectModel(@PathVariable int id) {
        try {
            service.selectModel(id);
            return Response.ok(null);
        } catch (NotFoundException e) {
            e.printStackTrace();
            return Response.error("Cannot find model id " + id);
        } catch (IOException e) {
            e.printStackTrace();
            return Response.error("Change failed");
        }
    }

    @Operation(summary = "删除指定模型")
    @DeleteMapping(value = "/model/{id}")
    public Response<Object> deleteModel(@PathVariable int id) {
        try {
            service.deleteModel(id);
            return Response.ok(null);
        } catch (IOException e) {
            e.printStackTrace();
            return Response.error("Write config file error.");
        }
    }

    @Operation(summary = "训练模型")
    @PostMapping(value = "/model/train")
    public Response<Object> trainModel(@RequestBody TrainParams params) {
        try {
            System.out.println(params);
            service.trainModel(params);
            return Response.ok(null);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Response.error("Error starting to train.");
        }
    }

    @Operation(summary = "获取训练进度")
    @GetMapping(value = "/model/train/progress/{modelName}")
    public Response<TrainingProgress> trainProgress(@PathVariable String modelName) {
        try {
            TrainingProgress trainingProgress = service.getTrainingProgress(modelName);
            System.out.println(trainingProgress);
            return Response.ok(trainingProgress);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error getting training progress");
        }
    }

    @Operation(summary = "提交反馈")
    @PostMapping(value = "/model/test/feedback")
    public Response<Object> feedback() {
        try {
            service.feedback();
            return Response.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error posting feedback.");
        }
    }
}
